<?
session_start();
include("classes/connect.php");
require_once('classes/class.jira.php');
include('config.php');
include 'paginate/libs/ps_pagination.php';
ini_set('display_errors', 'Off');

$post = $_GET['post'];
$assignee = $_GET['assignee'];
$status=$_GET['urlstatus'];
$status=html_entity_decode($status);
$search=$_GET['search'];
$search=html_entity_decode($search);
$priority=$_GET['priority'];

function get_time_ago($time_stamp)
{
    $time_difference = strtotime('now') - $time_stamp;

    if ($time_difference >= 60 * 60 * 24 * 365.242199)
    {
        /*
         * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 365.242199 days/year
         * This means that the time difference is 1 year or more
         */
        return get_time_ago_string($time_stamp, 60 * 60 * 24 * 365.242199, 'year');
    }
    elseif ($time_difference >= 60 * 60 * 24 * 30.4368499)
    {
        /*
         * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 30.4368499 days/month
         * This means that the time difference is 1 month or more
         */
        return get_time_ago_string($time_stamp, 60 * 60 * 24 * 30.4368499, 'month');
    }
    elseif ($time_difference >= 60 * 60 * 24 * 7)
    {
        /*
         * 60 seconds/minute * 60 minutes/hour * 24 hours/day * 7 days/week
         * This means that the time difference is 1 week or more
         */
        return get_time_ago_string($time_stamp, 60 * 60 * 24 * 7, 'week');
    }
    elseif ($time_difference >= 60 * 60 * 24)
    {
        /*
         * 60 seconds/minute * 60 minutes/hour * 24 hours/day
         * This means that the time difference is 1 day or more
         */
        return get_time_ago_string($time_stamp, 60 * 60 * 24, 'day');
    }
    elseif ($time_difference >= 60 * 60)
    {
        /*
         * 60 seconds/minute * 60 minutes/hour
         * This means that the time difference is 1 hour or more
         */
        return get_time_ago_string($time_stamp, 60 * 60, 'hour');
    }
    else
    {
        /*
         * 60 seconds/minute
         * This means that the time difference is a matter of minutes
         */
        return get_time_ago_string($time_stamp, 60, 'minute');
    }
}

function get_time_ago_string($time_stamp, $divisor, $time_unit)
{
    $time_difference = strtotime("now") - $time_stamp;
    $time_units      = floor($time_difference / $divisor);

    settype($time_units, 'string');

    if ($time_units === '0')
    {
        return 'less than 1 ' . $time_unit . ' ago';
    }
    elseif ($time_units === '1')
    {
        return '1 ' . $time_unit . ' ago';
    }
    else
    {
        /*
         * More than "1" $time_unit. This is the "plural" message.
         */
        // TODO: This pluralizes the time unit, which is done by adding "s" at the end; this will not work for i18n!
        return $time_units . ' ' . $time_unit . 's ago';
    }
}
if($priority==""){
	if($assignee!=""){//assignee has value
		if($post!=""){//post has value
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND post='".$post."' AND currentAssignee='".$assignee."' AND deleted='0' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
		else{//post all
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND currentAssignee='".$assignee."' AND deleted='0' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
	}
	else{
		if($post!=""){//post has value
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND post='".$post."' AND deleted='0' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
		else{//post all
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND deleted='0' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
	}
}
else{
	if($assignee!=""){//assignee has value
		if($post!=""){//post has value
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND post='".$post."' AND priority='".$priority."' AND currentAssignee='".$assignee."' AND deleted='0' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
			
		}
		else{//post all
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND currentAssignee='".$assignee."' AND priority='".$priority."' AND deleted='0' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
	}
	else{
		if($post!=""){//post has value
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND post='".$post."' AND deleted='0' AND priority='".$priority."' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
		else{//post all
			$sql="SELECT * from trn_emaildetails WHERE currentStatus='".$status."' AND deleted='0' AND priority='".$priority."' AND applicantName LIKE '%".$search."%' ORDER BY dateReceived DESC";
		}
	}
}



$query=mysql_query($sql);
$pager = new PS_Pagination( $conn, $sql, 20, 20, null );
$query= $pager->paginate(); 
$num = mysql_num_rows($query);


	$config->username= $username;
	$config->password= $pass;
	$config->port= "";
	$config->host= $host;
	
	$queryIssue = new Jira($config);
	$array=$queryIssue->queryPermission($project);
	$response = json_decode($array->responseBody);
        $arr_out = array();
		if($response->permissions->DELETE_ISSUE->havePermission==1){
			$admin=1;
		}
		else{
			$admin=0;
		}
if($num >= 1 ){    
    //looping through the records retrieved
//echo "<form action='deletedata.php' method='post'>";
while($row = mysql_fetch_array($query)){
?>
	
	<div class="listbox2">
	<?
	if($admin==1){	
	?>
		<div class="HMcheckbox lk"><input type="checkbox" value="<?=$row['id']?>:<?=$row['jiraTaskID']?>" name="deletedata[]" class="bulkdelete"/></div>
	<?}else{?>
		<div class="HMcheckbox lk">&nbsp;</div>
	<?}?>
	<a href="javascript:void(0);" onclick="javascript:popitup('edit.php?id=<?=$row['id']?>&jiraid=<?=$row['jiraTaskID']?>&currentstatus=<?=$row['currentStatus']?>&currentassignee=<?=$row['currentAssignee']?>&vcode=<?=$row['vcode']?>')">
		<div class="HMname lk"><?=$row['applicantName']?>&nbsp;</div>
		<div class="HMemail lk"><?=$row['email']?>&nbsp;</div>
		<div class="HMsubject lk"><?=$row['subject']?>&nbsp;</div>
		<div class="HMdate lk"><?=date("Y-m-d H:i",$row['dateReceived'])?>&nbsp;</div>
        <div class="HMdate lk"><?=date("Y-m-d H:i",$row['modifiedDate'])?>&nbsp;</div>
        <div class="HMassignee lk"><?=$row['modifiedBy']?>&nbsp;</div>
		<div class="HMstatus lk"><?=get_time_ago($row['dateReceived'])?>&nbsp;</div>
		<div class="HMpriority lk"><?=$row['priority']?>&nbsp;</div>
		<div class="HMassignee lk"><?=$row['currentAssigneeName']?>&nbsp;</div>
	</a>	
	</div>
	<?
        
        
          $arr=  array(
                    "Id"=> $i + 1,
                    "name" => $row['applicantName'],
                    "email" =>$row['email'],
                    "subject" =>$row['subject'],
                    "ReceivedDate" => $row['dateReceived'],
                    "ModifiedDate" => $row['modifiedDate'],
                    "modifyby" => $row['modifiedBy'],
                    "priority" => $row['priority'],
                   );
                $arr_out[] = $arr;
                $i++;
        }     
     echo json_encode($arr_out);
    echo "</table>";
}else{
    //if no records found
    echo "No records found!";
}
//page-nav class to control
//the appearance of our page
//number navigation

echo "<div class='page-nav'>";
    //display our page number navigation
    echo $pager->renderFullNav();
echo "</div>";	
?>
<?if($admin==1){
	if(mysql_num_rows($query)>0){
?>
<div class="deletebutton">
	<input type="submit" value="Delete" name="delete" id="delete">	
</div>	
<?}}?>
